export const login = (data)=>{
    return {
      type:"LOGIN",
      payload:data
    }
  }
  
  export const logoutFnc = (data)=>{
    return {
      type:"LOGOUT",
      payload:data
    }
  }

  export function logout(data,callback){
    return dispatch=>{
      dispatch(logoutFnc(data));
      callback();
    }
  }

  export function handleLogin(params={username:'',password:''},callback){
    return dispatch=>{
      fetch("/api/users/login",{
        method: "POST",
        headers: {
          'Content-Type':'application/x-www-form-urlencoded'
        },
        body: `name=${params.username}&password=${params.password}`
      }).then(res=>{
        return res.json();
      }).then((data) => {
        dispatch(login({'login':data,'username':params.username}));
        callback(data);
    })

    }
  }